package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.Basket;
import org.example.model.Result;
import org.example.service.BasketService;
import org.example.utils.AuthUtils;
import org.example.vo.CartTotalAmount;
import org.example.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(tags = "购物车业务接口管理")
@RestController
@RequestMapping("p/shopCart")
public class BasketController {

    @Autowired
    private BasketService basketService;


    @ApiOperation("查询会员购物车中商品数量")
    @GetMapping("/prodCount")
    public Result<Integer> loadMemberBasketProdCount() {
        String openId = AuthUtils.getMemberOpenId();
        Integer count = basketService.queryMemberBasketProdCount(openId);
        return Result.success( count);
    }

    @ApiOperation("查询会员购物车页面数据")
    @GetMapping("info")
    public Result<CartVo> loadMemberCartVo(){
        CartVo cartVo = basketService.queryMemberCartVo();
        return Result.success( cartVo);
    }

    @ApiOperation("计算会员选中购物车中商品的金额")
    @PostMapping("/totalPay")
    public Result<CartTotalAmount> calculateMemberCheckedBasketTotalAmount(@RequestBody List<Long> basketIds) {
        CartTotalAmount cartTotalAmount = basketService.calculateMemberCheckedBasketTotalAmount(basketIds);
        return Result.success(cartTotalAmount);
    }

    @ApiOperation("添加商品到购物车或修改商品在购物车中的数量")
    @PostMapping("/changeItem")
    public Result<String> changeCartItem(@RequestBody Basket basket) {
        Boolean changed = basketService.changeCartItem(basket);
        return Result.handle( changed);
    }

    @ApiOperation("删除会员选中的购物车记录")
    @DeleteMapping("/deleteItem")
    public Result<String> removeMemberCheckedBasket(@RequestBody List<Long> basketIds) {
        boolean removed = basketService.removeBatchByIds( basketIds);
        return Result.handle( removed);
    }

    // TODO feign接口
    @GetMapping("/getBasketListByIds")
    public Result<List<Basket>> getBasketListByIds(@RequestParam List<Long> ids){
        List<Basket> basketList = basketService.listByIds(ids);
        return Result.success(basketList);
    }

    @DeleteMapping("/removeBasketByOpenIdAndSkuIds")
    public Result<Boolean> removeBasketByOpenIdAndSkuIds(@RequestBody Map<String, Object> param) {
        // 获取会员openId
        String openId = (String) param.get("openId");
        // 获取商品skuId集合
        List<Long> skuIdList = (List<Long>) param.get("skuIdList");
        // 根据会员openId和商品skuId集合删除购物车记录
        boolean removed = basketService.remove( new LambdaQueryWrapper<Basket>()
                .eq(Basket::getOpenId, openId)
                .in( Basket::getSkuId,  skuIdList));
        return Result.success( removed);
    }

}
